package org.example.myleet.p1154;

public class Solution {

    //每个月的天数，0是预留占位，从1开始算
    private static final int[] DAYS_OF_MONTH = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public int dayOfYear(String date) {
        //解析年月日
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = Integer.parseInt(date.substring(8, 10));
        //计算是否闰年。如果year是100的倍数，则year需要是400的倍数才是闰年；否则非整百情况下只要是4的倍数就是闰年
        boolean leapYear = false;
        if (year % 100 == 0) {
            if (year % 400 == 0) {
                leapYear = true;
            }
        } else {
            if (year % 4 == 0) {
                leapYear = true;
            }
        }
        //本月的天数
        int countOfDay = day;
        for (int i = 1; i < month; ++i) {
            //前面月份逐月累加天数
            countOfDay += DAYS_OF_MONTH[i];
        }
        if (month > 2 && leapYear) {
            //闰年补充1天
            ++countOfDay;
        }
        return countOfDay;
    }
}
